package com.ruoyi.web.controller.fantasy;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.huanzhidadi.fantasy.bean.domain.FantasyNotice;
import com.ruoyi.fantasy.service.IFantasyNoticeService;

/**
 * 公告 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-23
 */
@Controller
@RequestMapping("/fantasy/fantasyNotice")
public class FantasyNoticeController extends BaseController
{
    private String prefix = "fantasy/fantasyNotice";
	
	@Autowired
	private IFantasyNoticeService fantasyNoticeService;
	
	@RequiresPermissions("fantasy:fantasyNotice:view")
	@GetMapping()
	public String fantasyNotice()
	{
	    return prefix + "/fantasyNotice";
	}
	
	/**
	 * 查询公告列表
	 */
	@RequiresPermissions("fantasy:fantasyNotice:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FantasyNotice fantasyNotice)
	{
		startPage();
		//查询列表时，这个字段为空字符串的话有问题 wanghui 2019.1.6
		if (fantasyNotice.getNoticeType()!=null && fantasyNotice.getNoticeType().equals("")) {
			fantasyNotice.setNoticeType(null);
		}
        List<FantasyNotice> list = fantasyNoticeService.selectFantasyNoticeList(fantasyNotice);
		fantasyNoticeService.transforData(list);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出公告列表
	 */
	@RequiresPermissions("fantasy:fantasyNotice:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyNotice fantasyNotice)
    {
    	List<FantasyNotice> list = fantasyNoticeService.selectFantasyNoticeList(fantasyNotice);
        ExcelUtil<FantasyNotice> util = new ExcelUtil<FantasyNotice>(FantasyNotice.class);
        return util.exportExcel(list, "fantasyNotice");
    }
	
	/**
	 * 新增公告
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存公告
	 */
	@RequiresPermissions("fantasy:fantasyNotice:add")
	@Log(title = "公告", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FantasyNotice fantasyNotice)
	{
		// 取身份信息
		SysUser user = ShiroUtils.getSysUser();
		fantasyNotice.setCreateBy(user.getUserId().toString());
		fantasyNotice.setCreateTime(new Date());
		fantasyNotice.setUpdateBy(user.getUserId().toString());
		fantasyNotice.setUpdateTime(new Date());
		return toAjax(fantasyNoticeService.insertFantasyNotice(fantasyNotice));
	}

	/**
	 * 修改公告
	 */
	@GetMapping("/edit/{noticeId}")
	public String edit(@PathVariable("noticeId") Integer noticeId, ModelMap mmap)
	{
		FantasyNotice fantasyNotice = fantasyNoticeService.selectFantasyNoticeById(noticeId);
		mmap.put("fantasyNotice", fantasyNotice);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存公告
	 */
	@RequiresPermissions("fantasy:fantasyNotice:edit")
	@Log(title = "公告", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FantasyNotice fantasyNotice)
	{
		SysUser user = ShiroUtils.getSysUser();
		fantasyNotice.setUpdateBy(user.getUserId().toString());
		fantasyNotice.setUpdateTime(new Date());
		return toAjax(fantasyNoticeService.updateFantasyNotice(fantasyNotice));
	}
	
	/**
	 * 删除公告
	 */
	@RequiresPermissions("fantasy:fantasyNotice:remove")
	@Log(title = "公告", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fantasyNoticeService.deleteFantasyNoticeByIds(ids));
	}


	/**
	 * 禁用城市
	 */
	@RequiresPermissions("fantasy:fantasyNotice:disable")
	@Log(title = "城市", businessType = BusinessType.DISABLE)
	@PostMapping( "/disable")
	@ResponseBody
	public AjaxResult disable(String ids)
	{
		return toAjax(fantasyNoticeService.disableFantasyNoticeByIds(ids));
	}

	/**
	 * 还原城市
	 */
	@RequiresPermissions("fantasy:fantasyNotice:disable")
	@Log(title = "城市", businessType = BusinessType.RESTORE)
	@PostMapping( "/restore")
	@ResponseBody
	public AjaxResult restore(String ids)
	{
		return toAjax(fantasyNoticeService.restoreFantasyNoticeByIds(ids));
	}
}
